//
//  MoreCompanyCommentViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/15.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MoreCompanyCommentViewController.h"
#import "MoreCompanyCommentCell.h"
#import "UITableView+Empty.h"

@interface MoreCompanyCommentViewController ()

@property (nonatomic, strong) NSMutableArray *commentArray;

@end

@implementation MoreCompanyCommentViewController {
    NSInteger _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"员工点评";
    
    [self setupSubViews];

    [self.tableView.mj_header beginRefreshing];
}

- (void)setupSubViews {
    self.tableView.backgroundColor = kStyleColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"MoreCompanyCommentCell" bundle:nil] forCellReuseIdentifier:@"MoreCompanyCommentCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self loadListData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNo += 1;
        [self loadListData];
    }];
}

- (void)loadListData {
    WS(weakSelf)
    [SendRequest getCompanyIncumbentCommentsByCompanyId:self.companyId andPageNo:[NSString stringWithFormat:@"%zd", _pageNo] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询公司点评列表 = %@", [[result objectForKey:@"data"] objectForKey:@"list"]);
            
            NSArray *datas = [CompanyCommentModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            
            if (_pageNo == 1) {
                [weakSelf.commentArray removeAllObjects];
                [weakSelf.commentArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                if (datas.count == 0) {
                    [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:150];
                } else {
                    [weakSelf.tableView haveDataRemoveEmptyUI];
                }
            } else {
                [weakSelf.commentArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                if (datas.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MoreCompanyCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreCompanyCommentCell"];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.model = [self.commentArray objectAtIndex:indexPath.row];
    return cell;
}

- (NSMutableArray *)commentArray {
    if (!_commentArray) {
        _commentArray = [[NSMutableArray alloc] init];
    }
    return _commentArray;
}

@end
